﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ncp.Business.SingleManager
{
    public class SinglePageBll
    {
        /// <summary>
        /// 得到新闻记录数
        /// </summary>
        public static int GetSingleCount(string pcWhere, string pDataBase)
        {
            string sqlStr = "select count(*) from bi_SinglePage_v";
            if (!string.IsNullOrEmpty(pcWhere))
                sqlStr += " where " + pcWhere;
            return CommonBll.ReadInteger(sqlStr, pDataBase);
        }
        /// <summary>
        /// 得到新闻信息表
        /// </summary>
        public static DataTable GetSingleTable(string fieldList, Dictionary<string, object> dicWhere, string order, string pDataBase)
        {
            List<SqlParameter> ilist = new List<SqlParameter>();
            string pcWhere = "1=1 ";
            foreach (KeyValuePair<string, object> kvp in dicWhere)
            {
                if (kvp.Key != "more")
                {
                    pcWhere += " and " + kvp.Key + "=@" + kvp.Key;
                    ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
                }
                else
                {
                    pcWhere += " and " + kvp.Value;
                }
            }
            string sqlStr = "select " + fieldList + " from bi_SinglePage_v where " + pcWhere + " " + order;
            SqlParameter[] paras = ilist.ToArray();
            return CommonBll.GetdtList(sqlStr, pDataBase, paras);
        }

        /// <summary>
        /// 得到分页新闻信息表
        /// </summary>
        public static DataTable GetSingleTable(int piPageIndex, int piPageSize, string pcFieldList, string pcOrderBy, Dictionary<string, object> dicWhere, string pDataBase)
        {
            List<SqlParameter> ilist = new List<SqlParameter>();
            string pcWhere = "1=1 ";
            foreach (KeyValuePair<string, object> kvp in dicWhere)
            {
                pcWhere += " and " + kvp.Key + "=@" + kvp.Key;
                ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
            }
            string sqlStr = CommonBll.GetPagingSQL(piPageIndex, piPageSize, "bi_SinglePage_v", pcFieldList, "SingleClass_Id", pcOrderBy, pcWhere);
            SqlParameter[] paras = ilist.ToArray();
            return CommonBll.GetdtList(sqlStr, pDataBase, paras);
        }

        /// <summary>
        /// 删除新闻
        /// </summary>
        public static int DeleteSingle(string News_Id, string pDataBase)
        {
            string sqlStr = "delete from bi_SinglePage where Single_Id in(" + News_Id + ")";
            return CommonBll.ExecuteNonQuery(sqlStr, pDataBase);
        }

        /// <summary>
        /// 修改
        /// </summary>
        public static int UpdateSingle(Dictionary<string, object> dicMessage, Dictionary<string, object> dicWhere, string pDataBase)
        {
            List<SqlParameter> ilist = new List<SqlParameter>();
            string field = "", where = "";
            if (dicMessage.Count == 0)
            {
                return 0;
            }
            foreach (KeyValuePair<string, object> kvp in dicMessage)
            {
                field += "," + kvp.Key + "=@" + kvp.Key;
                ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
            }
            foreach (KeyValuePair<string, object> kvp in dicWhere)
            {
                where += " and " + kvp.Key + "=@" + kvp.Key;
                ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
            }
            SqlParameter[] paras = ilist.ToArray();
            string sqlStr = "UPDATE dbo.bi_SinglePage set " + field.Substring(1) + " where 1=1 " + where;
            return CommonBll.ExecuteNonQuery(CommandType.Text, sqlStr, pDataBase, paras);
        }

        public static int InsertSingle(Dictionary<string, object> dicMessage,string pDataBase)
        {
            List<SqlParameter> ilist = new List<SqlParameter>();
            string field = "", values = "", update = "";
            if (dicMessage.Count == 0)
            {
                return 0;
            }
            foreach (KeyValuePair<string, object> kvp in dicMessage)
            {
                field += "," + kvp.Key;
                values += ",@" + kvp.Key;
                update += "," + kvp.Key + "=@" + kvp.Key;
                ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
            }
            SqlParameter[] paras = ilist.ToArray();
            string sql = " IF EXISTS(select * from bi_SinglePage where Single_Class= @Single_Class) ";
            string sqlupdate = "UPDATE dbo.bi_SinglePage set " + update.Substring(1) + " where Single_Class= @Single_Class ";
            string sqlStr = " else insert into dbo.bi_SinglePage (" + field.Substring(1) + ") values (" + values.Substring(1) + ")";
            return CommonBll.ExecuteNonQuery(CommandType.Text, sql+sqlupdate+sqlStr, pDataBase, paras);
        }

    }
}
